<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */

namespace Admin\Controller;

use Admin\Controller\AdminController;
use Tool\Api\AreaApi;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;

class AreaController extends AdminController{

	protected function _initialize(){
		parent::_initialize();
	}

	/**
	 * show province list
	 * @return [type] [description]
	 */
	public function index($father=0){
		$this->assignTitle('地区管理');
		$r = apiCall(AreaApi::QUERY,array(array('father'=>$father),array('curpage'=>$p,'size'=>10)));
		// dump($r);exit;
		if(!$r['status']) $this->error($r['info']);
		$this->assign('father',$father);
		$this->assign('list',$r['info']['list']);
		$this->assign('show',$r['info']['show']);
		$this->display('Area/index_a');
	}

	public function delete($id){
		$r = apiCall(AreaApi::DELETE,array(array('id'=>$id)));
		// dump($r);exit;
		if(!$r['status']) $this->error($r['info']);
		else $this->success('修改成功');
	}

	public function add($father){
		$r = apiCall(CityApi::GET_INFO,array(array('cityID'=>$father)));
		if(!$r['status']) $this->error($r['info']);
		if(IS_GET){
			$this->assign('entry',$r['info']);
			$this->display('Area/add_a');
		}elseif(IS_AJAX){
			$map           = array();
			$map['areaID'] = I('post.areaID');
			$map['area']   = I('post.area');
			$map['father'] = $father;
			$this->upd($map,$father);
		}
	}
	public function edit($id){
		$r = apiCall(AreaApi::GET_INFO,array(array('id'=>$id)));
		if(!$r['status']) $this->error($r['info']);
		if(IS_GET){
			$this->assign('entry',$r['info']);
			$this->display('Area/edit_a');
		}elseif(IS_AJAX){
			$map           = array();
			$map['areaID'] = I('post.areaID');
			$map['area']   = I('post.area');
			$this->upd($map,$r['info']['father'],$id);
		}
	}

	protected function upd($map,$father,$id=0){
		$pre = substr($father,0,4);
		// dump($map);exit;
		if(!preg_match('/[\x{4e00}-\x{9fa5}]+/u', $map['area'])) $this->error('名称格式错误');
		if(!preg_match('/^'.$pre.'[A-Za-z0-9]{2}$/', $map['areaID'])  || substr($map['areaID'],4,2)=='00') $this->error('编号格式错误');
		unset($pre);
		$where               = array();
		$where['areaID']     = $map['areaID'];
		$where['area']       = $map['area'];
		$where['_logic']     = 'OR';

		$where2['_complex'] = $where;
		$where2['father']   = $father;
		if($id){
			$where2['id']   = array('neq'=>$id);
		}else{
			$where2 = $where;
		}
		$r = apiCall(AreaApi::GET_INFO,array($where2));
		unset($where);
		// dump(M()->getLastSql());exit;
		if(!$r['status']) $this->error('AreaApi get error');
		if($r['info']){
			if($r['info']['areaid'] == $map['areaID']) $this->error('该编号已存在');
			if($r['info']['area']   == $map['area']) $this->error('该名字已存在');
		}
		if($id){
			$r = apiCall(AreaApi::SAVE_BY_ID,array($id,$map));
			if(!$r['status']) $this->error('修改失败');
			$this->success('修改成功',U('Area/index',array('father'=>$father)));
		}else{
			$r = apiCall(AreaApi::ADD,array($map));
			if(!$r['status']) $this->error('添加失败');
			$this->success('修改成功',U('Area/index',array('father'=>$father)));
		}
	}
}